import { Database, Project } from '@/types/domain_models'
import { ApplicationYaml } from '@/types/yaml'
import TianzhiJavaGenerator from '.'
import { stringify } from 'yaml'

const databaseType = {
  mysql: 'com.mysql.cj.jdbc.Driver',
  oracle: 'oracle.jdbc.driver.OracleDriver',
}

/**
 * 写入 YAML 文件
 */
export function writing(generator: TianzhiJavaGenerator, packageName: string, database: Database) {
  const applicationYaml: Partial<ApplicationYaml> = {
    spring: {
      datasource: {
        url: database.url,
        username: database.username,
        password: database.password,
        'driver-class-name': databaseType[database.type],
      },
    },
    mybatis: {
      'type-aliases-package': `${packageName}.model`,
      mapperLocations: 'classpath:mapper/*.xml',
    },
  }

  const yaml = stringify(applicationYaml)
  generator.fs.write(generator._resourceFilePath('application.yml'), yaml)
}
